var ajaxListUrl = TriageUrl+'/broadcast/listBroadcastWithPage';// 获取列表的接口
var ajaxEditUrl =  TriageUrl+'/broadcast/editBroadcast';// 添加的接口
var ajaxAddUrl =  TriageUrl+'/broadcast/addBroadcast';// 修改的接口
var ajaxPlayUrl =  TriageUrl+'/broadcast/playBroadcast';// 播放的接口
var ajaxDelUrl =  TriageUrl+'/broadcast/removeBroadcast';// 删除的接口
var userId = getCookie('userId');
var hospitalId = getCookie('hospitalId');
var maxLength = 100;
var mocks = {
    "status":0,
    "desc":"操作成功",
    "data":{
        "pageNum":1,
        "pageSize":10,
        "size":4,
        "startRow":1,
        "endRow":4,
        "total":4,
        "pages":1,
        "list":[
            {
                "broadcastId":"cebf756b21e7460d82df87bed648b58c",
                "content":"请保持安静，谢谢！",
                "editor":"",
                "editTime":"20201112161431",
                "isDeleted":0,
                "hospitalId":"4e66fabafd944e458dc259ce890c31f3",
                "createTime":"20201112161431",
                "creator":""
            },
            {
                "broadcastId":"4341cfa114b64052a7e4033a6784e000",
                "content":"请保持安静，谢谢大家！",
                "editor":"",
                "editTime":"20201112161657",
                "isDeleted":0,
                "hospitalId":"4e66fabafd944e458dc259ce890c31f3",
                "createTime":"20201112161657",
                "creator":""
            },
            {
                "broadcastId":"79a0f4b42d5847d4822379226569eb26",
                "content":"请保持安静，谢谢大家！",
                "editor":"",
                "editTime":"20201112162215",
                "isDeleted":0,
                "hospitalId":"4e66fabafd944e458dc259ce890c31f3",
                "createTime":"20201112162215",
                "creator":""
            },
            {
                "broadcastId":"14729f663ee143559a91bd921c5831e8",
                "content":"请保持安静，谢谢大家！",
                "editor":"",
                "editTime":"20201112162536",
                "isDeleted":0,
                "hospitalId":"4e66fabafd944e458dc259ce890c31f3",
                "createTime":"20201112162536",
                "creator":""
            }
        ],
        "firstPage":1,
        "prePage":0,
        "nextPage":0,
        "lastPage":1,
        "isFirstPage":true,
        "isLastPage":true,
        "hasPreviousPage":false,
        "hasNextPage":false,
        "navigatePages":8,
        "navigatepageNums":[
            1
        ]
    }
};
$(function () {
   //获取列表
    getAllList();
    $('#text_content').keyup(function () {
        var len = $(this).val().length;
        var strResult;
        if (len > maxLength) {	//如果输入的字数超过了限制
            strResult = '<span style="color: red">输入内容已超出' + (len - maxLength) + '字</span>';
        }
        else {
            strResult = '<span>你还可以输入' + (maxLength - len) + '字</span>';
        }
        $(".textareaTip").html(strResult);
    });
});

function getAllList(type) {
    layer.msg("加载中", { icon: 16 });
    type = type || 'refresh';
    if(type=='refresh') $('#LAY_flow').empty();
    layui.use('flow', function(){
        var flow = layui.flow;
        flow.load({
            elem: '#LAY_flow' //流加载容器
            ,scrollElem: '#LAY_flows' //滚动条所在元素，一般不用填，此处只是演示需要。
            ,done: function(page, next){ //执行下一页的回调
                //模拟数据插入
               var timer =  setTimeout(function(){
                   $.ajax({
                       url: ajaxListUrl,
                       type: "get",
                       data: {
                           pageNumber: page,
                           pageSize: 10
                       },
                       headers:{
                           userId:userId,
                           hospitalId:hospitalId
                       },
                       success: function (res) {
                           layer.closeAll();
                           res = JSON.parse(res);
                           if(res.status==0){
                               var frag = [] ;
                                var arr = res.data.list;
                                for (var i = 0,lens =arr.length ;i<lens;i++){
                                    if(arr[i]=='') continue;
                                    frag .push('<div class="flow-ex">') ;
                                    frag .push('<div class="flow-ex-cell">'+((i+1)+10*(page-1))+'</div>') ;
                                    frag .push('<div class="flow-ex-cell">'+(arr[i]['content'])+'</div>') ;
                                    frag .push('<div class="flow-ex-cell" data-id="'+arr[i]['broadcastId']+'" data-info="'+arr[i]['content']+'"><span class="layui-btn layui-btn-sm" onclick="playVoice(this)" >播放</span><span class="layui-btn layui-btn-sm layui-btn-warm" onclick="editVoice(this)">修改</span><span class="layui-btn layui-btn-sm layui-btn-danger" onclick="delVoice(this)">删除</span></div>') ;
                                    frag .push('</div>') ;
                                }
                               next(frag.join(''), page < res.data.pages); //假设总页数为 10
                           }
                       }
                   });
                }, 500);
               timer = null;
            }
        });
    });
}

/**
 *  修改语音
 *  @param elem ID
 * */
function editVoice(elem) {
    var obj = $(elem).parent('div').data();
    $("#text_content").val(obj.info);
    layer.open({
        type: 1,
        title: false,
        btn: ['确定', '取消'],
        shadeClose: true,
        area: ['500px', '240px'],
        skin: 'yourclass',
        content: $(".layui-mode"),
        yes: function (index, layero) {
            var content = $("#text_content").val()||'';
            if(content=='')return  layer.msg('内容不能为空！');
            if(content.length>maxLength) return  layer.msg('输入内容超过限制！');
            $.ajax({
                url: ajaxEditUrl,
                type: "POST",
                data: {
                    broadcastId:obj.id,
                    content: content
                },
                headers:{
                    userId:userId,
                    hospitalId:hospitalId
                },
                success: function (res) {
                    res = JSON.parse(res);
                    if(res.status==0){
                        getAllList();
                        layer.msg('修改成功！');
                    }else {
                        layer.msg(res.desc);
                    }
                }
            });
        },
        cancel: function(){
            $(".textareaTip").html('');
        },
        end: function(){
            $(".textareaTip").html('');
        }
    });
}
/**
 *  播放
 *  @param elem ID
 * */
function playVoice(elem) {
    var _triageId = unescape(getCookie("deviceId"));
    var obj = $(elem).parent('div').data();
    $.ajax({
        url: ajaxPlayUrl,
        type: "POST",
        data: {
            content: obj.info,
            deviceId:_triageId
        },
        headers:{
            userId:userId,
            hospitalId:hospitalId
        },
        success: function (res) {
            res = JSON.parse(res);
            if(res.status==0){
                layer.msg('播放成功！');
            }else {
                layer.msg(res.desc);
            }
        },
        error:function (e) {
            layer.msg('播放失败：'+e.message);
        }
    });
}

/**
 *  删除
 *  @param elem ID
 * */
function delVoice(elem) {
    var obj = $(elem).parent('div').data();
    layer.confirm('删除之后就无法恢复，是否继续？', {
        btn: ['继续','取消'] //按钮
    }, function(){
        $.ajax({
            url: ajaxDelUrl,
            type: "POST",
            data: {
                broadcastId: obj.id
            },
            headers:{
                userId:userId,
                hospitalId:hospitalId
            },
            success: function (res) {
                res = JSON.parse(res);
                if(res.status==0){
                    getAllList();
                    layer.msg('删除成功！');
                }else {
                    layer.msg(res.desc);
                }
            }
        });
    }, function(){

    });
}
/**
 *  添加
 *  @param elem ID
 * */
function addVoice() {
    $("#text_content").val('');
    layer.open({
        type: 1,
        title: false,
        btn: ['确定', '取消'],
        shadeClose: true,
        area: ['500px', '240px'],
        skin: 'yourclass',
        content: $(".layui-mode"),
        yes: function (index, layero) {
            var content = $("#text_content").val()||'';
            if(content=='') return  layer.msg('内容不能为空！');
            if(content.length>maxLength) return  layer.msg('输入内容超过限制！');
            $.ajax({
                url: ajaxAddUrl,
                type: "POST",
                data: {
                    content: content
                },
                headers:{
                    userId:userId,
                    hospitalId:hospitalId
                },
                success: function (res) {
                    res = JSON.parse(res);
                    if(res.status==0){
                       layer.msg('添加成功！', { shift: -1},function () {
                           getAllList();
                       });
                    }else {
                        layer.msg(res.desc);
                    }
                }
            });
        },
        cancel: function(){
            $(".textareaTip").html('');
        },
        end: function(){
            $(".textareaTip").html('');
        }
    });
}
